#！/bin/bash

time=$(date +%Y%m%d%H%M%S)
log_dir="./log/log_${time}"
# 检查log_backup目录是否存在，不存在则创建
if [[ ! -d "log_backup" ]]; then
    mkdir log_backup
    echo "Created log_backup directory"
else
    echo "log_backup directory already exists"
fi
mv ./log/* log_backup/
rm -rf ./log/*

# 创建日志目录
mkdir -p $log_dir

# get all mindie pods name and namespace
pods=$(kubectl get pods -A | grep "mindie-" | awk '{print $1 " " $2}')

if [[ -z "$pods" ]]; then
    echo "No mindie pods found"
    exit 1
fi

echo "$pods" | while read -r namespace pod_name; do
    logfile="$log_dir/$pod_name.log"

    echo "Collecting logs for $pod_name in $namespace"
    kubectl logs $pod_name -n $namespace -f > "$logfile" 2>&1 &
done

echo "Logs collected for $pods"
echo "Logs are saved in $log_dir"